<?php

namespace App\Console\Commands;

use App\AdvertiserVideo;
use App\Service\DunVideoService;
use Illuminate\Console\Command;

class ScheduleVideo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'schedule:video';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $dunVideoService = DunVideoService::getInstance();
        $dunVideoService->setConfig(
            \config('app.dun_video_secret_id'),
            \config('app.dun_video_secret_key')
        );

        AdvertiserVideo::query()->where('status', '=',0)->chunkById(100, function ($dataList) use ($dunVideoService){
            $dataList = $dataList->toArray();

            foreach ($dataList as $item){
                if ($item['status'] != 0 || empty($item['preview_url'])) {
                    continue;
                }
                $dunVideoService->main([
                    "url" => $item['preview_url'],
                    'account' => $item['account_id'],
                    "dataId" => $item['video_id'],
                    'callbackUrl' => config('app.url') . '/api/yd/video/callback'
                ]);
                usleep(1000);
            }
        });
        return 0;
    }
}
